package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 生生不息<br/>
 * 嗲嗲的声音为己方全体员工增加一个等同于现有气势值{0=50#0}%的护盾，这个护盾每回合恢复自身{1=10#0}%气势，直到被击破或自动解除
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90108 extends CombatSkill {

	public COMBAT_SKILL_90108(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		List<CombatUnit> units = action.unit.getTeam().all();
		for (int i = 0; i < units.size(); i++) {
			CombatUnit unit = units.get(i);
			if (!unit.isDied()) {
				action.addBuff(new BUFF(skill, unit));
				action.addBuff(new BUFF2(skill, unit));
			}
		}
	}
	
	// 护盾
	static class BUFF extends CombatBuff {
		int hp;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner);
			this.hp = (int) (owner.getHp() * skill.getParameter(0) / GameStaticConfig.percentBase);
		}
		@Override
		public void roundStart(Round round) {
		}
		@Override
		public void preDamage(ActionEffect effect) {
			active = true;
			int delta = effect.getHpDelta();
			if (delta < 0 && hp > 0) {
				hp += delta;
				Value v = effect.getValue(CombatAttributeType.HP);
				v.setDelta(hp > 0 ? 0 : hp);
			}
		}
		@Override
		public void roundEnd(Round round) {
			if (hp <= 0) { // 破盾
				uneffect();
				round.addBuff(this);
			}
		}
		@Override
		public void preAction(Action action) {
		}
	}
	
	// 回血
	static class BUFF2 extends CombatBuff {
		int refBuffId;
		int recoverHp;
		public BUFF2(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(1), owner);
			this.refBuffId = skill.getTemplate().getCombatBuff().getId();
			this.recoverHp = (int) (owner.getMaxHp() * skill.getParameter(1) / GameStaticConfig.percentBase);
		}
		@Override
		public void roundStart(Round round) {
		}
		@Override
		public void preDamage(ActionEffect effect) {
		}
		@Override
		public void roundEnd(Round round) {
			CombatBuff ref = owner.getBuff(refBuffId);
			if (ref == null || ref.getState() == CombatBuff.STATE_REMOVED) { // 破盾
				uneffect();
				round.addBuff(this);
			}
		}
		@Override
		public void preAction(Action action) {
			active = true;
			ActionEffect effect = action.addTarget(owner);
			effect.effect(CombatAttributeType.HP, recoverHp);
		}
	}

}
